package com.infoland.afanmall.model.fuPin.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.layoutManager.GridSpacingItemDecoration;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.fuPin.adapter.FpInfoAdapter;
import com.infoland.afanmall.model.fuPin.entity.FpCunInfoGoods;
import com.infoland.afanmall.model.fuPin.entity.FpCunInfoMessage;
import com.infoland.afanmall.model.youXuan.activity.GoodInfoActivity;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.stone.card.library.CardAdapter;
import com.stone.card.library.CardSlidePanel;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.vondear.rxtools.RxTool.getContext;

/**
 * @author 王永吉
 * @date 2018/7/3 11:25
 * 扶贫村详情
 * 商品列表
 */
public class FpCunInfoActivity extends AppCompatActivity {
    @BindView(R.id.image_slide_panel)
    CardSlidePanel imageSlidePanel;
    @BindView(R.id.header)
    MaterialHeader header;
    @BindView(R.id.fu_pin_refresh)
    SmartRefreshLayout fuPinRefresh;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.sd)
    ImageView sd;
    @BindView(R.id.fp_include_cun_title)
    LinearLayout fpIncludeCunTitle;
    @BindView(R.id.avi_pt)
    AVLoadingIndicatorView aviPt;
    int fupincunId;
    ArrayList<String> arrayList;
    @BindView(R.id.fu_pin_in_fo_txt)
    TextView fuPinInFoTxt;
    @BindView(R.id.fu_pin_in_fo_recycle)
    RecyclerView fuPinInFoRecycle;
    FpInfoAdapter fpInfoAdapter;
    @BindView(R.id.fu_pib_cun_name)
    TextView fuPibCunName;

    private int getLayout() {
        return R.layout.fu_pin_info;
    }

    //todo 初始化
    private void initData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            refLoadStart();
            initPage();
            fupincunId = extras.getInt("fupincunId");
            getCunZiInfo(fupincunId);
            /*** 加载更多**/
            fuPinRefresh.setOnLoadMoreListener(refreshLayout -> {
                refLoadStart();
                getCunZiGoods(fupincunId);
            });
        }
    }

    //   todo  /*** 查询村子详情**/
    private void getCunZiInfo(int fupincunId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fupincunId", fupincunId);
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("查询村子详情 ---->   ", "  " + response200);
                    FpCunInfoMessage fpCunInfoMessage = JSONObject.parseObject(response200, FpCunInfoMessage.class);
                    getCunZiGoods(fupincunId);
                    setCunZiInfo(fpCunInfoMessage);

                },
                success401 -> {
                    refLoadError();
                },
                error -> {
                    refLoadError();
                }
        ).post(CONST.FU_PIN_CUN_INFO, jsonObject, this);
    }

    //    todo /*** 查询村子,商品**/
    private void getCunZiGoods(int fupincunId) {
        if (getPage()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageNumber", 1);
            jsonObject.put("pageSize", CONST.FU_PIN_CUN_GOOD_SIZE);
            jsonObject.put("fupincunId", fupincunId);

            new OkHttpKit().send(
                    response200 -> {
                        FpCunInfoGoods fpCunInfoGoods = JSONObject.parseObject(response200, FpCunInfoGoods.class);
                        refLoadOk();
                        setCunZiGoods(fpCunInfoGoods);
                        LogKit.E("查询村子商品 ---->   ", "  " + response200);
                    },
                    success401 -> {
                        refLoadError();
                    },
                    error -> {
                        refLoadError();
                    }
            ).post(CONST.FU_PIN_CUN_GOODS, jsonObject, this);
        }

    }

    //    todo  设置村子详情
    private void setCunZiInfo(FpCunInfoMessage fpCunInfoMessage) {
        if (fpCunInfoMessage.isSuccess()) {
            setCards(fpCunInfoMessage);
            fuPibCunName.setText(fpCunInfoMessage.getObject().getName());
//            fuPinInFoTxt.setText();
        } else {
        }
//
    }

    //    todo /*** 设置 商品**/
    private void setCunZiGoods(FpCunInfoGoods fpCunInfoGoods) {
        if (fpCunInfoGoods.isSuccess()) {
            setPage(fpCunInfoGoods.getObject().getPageNumber(), fpCunInfoGoods.getObject().isLastPage());
            fpInfoAdapter = new FpInfoAdapter(R.layout.fu_pin_good_item, fpCunInfoGoods.getObject().getList());
            fpInfoAdapter.setOnItemClickListener((adapter, view, position) -> {
                FpCunInfoGoods.ObjectBean.ListBean o = (FpCunInfoGoods.ObjectBean.ListBean) adapter.getData().get(position);
                Bundle b = new Bundle();
                b.putInt("id", o.getId());
                /*** 去品详情页**/
                ToolsKit.go(GoodInfoActivity.class, b);
            });
            fuPinInFoRecycle.setAdapter(fpInfoAdapter);
        } else {
        }
    }

    int pageNumber = 1;
    boolean lastPAge = false;

    private void initPage() {
        this.pageNumber = 1;
        this.lastPAge = false;
    }

    private void setPage(int pageNumber, boolean lastPAge) {
        this.pageNumber = (pageNumber + 1);
        this.lastPAge = lastPAge;
    }

    private boolean getPage() {
        if (lastPAge) {
            refLoadError();
            ToastUtils.showShort("已经是最后一页");
            fuPinRefresh.setEnableRefresh(false);
            fuPinRefresh.setEnableLoadMore(false);
            return false;
        } else {
            return true;
        }
    }

    //    todo  加载或刷新失败
    private void refLoadError() {
        fuPinRefresh.setEnableRefresh(false);
        fuPinRefresh.setEnableLoadMore(true);
        fuPinRefresh.finishRefresh(500);
        fuPinRefresh.finishLoadMore();
        aviPt.setVisibility(View.GONE);
    }

    //   todo  加载或刷新完成*
    private void refLoadOk() {
        fuPinRefresh.setEnableRefresh(false);
        fuPinRefresh.setEnableLoadMore(true);
        fuPinRefresh.finishRefresh(500);
        fuPinRefresh.finishLoadMore();
        aviPt.setVisibility(View.GONE);
    }

    //    todo 加载或刷新开始
    private void refLoadStart() {
        fuPinRefresh.setEnableRefresh(false);
        fuPinRefresh.setEnableLoadMore(false);
        aviPt.setVisibility(View.VISIBLE);
    }

    //    todo 设置翻牌效果
    private void setCards(FpCunInfoMessage fpCunInfoMessage) {
        arrayList = new ArrayList<>();
        arrayList.add(fpCunInfoMessage.getObject().getF_url());
        arrayList.add(fpCunInfoMessage.getObject().getF_url());
        arrayList.add(fpCunInfoMessage.getObject().getF_url());
        arrayList.add(fpCunInfoMessage.getObject().getF_url());
        imageSlidePanel.setCardSwitchListener(new CardSlidePanel.CardSwitchListener() {
            @Override
            public void onShow(int index) {
            }

            @Override
            public void onCardVanish(int index, int type) {
                arrayList.add(arrayList.get(index));
            }
        });
        imageSlidePanel.setAdapter(new CardAdapter() {
            @Override
            public int getLayoutId() {
                return R.layout.test_card_item;
            }

            @Override
            public int getCount() {
                return arrayList.size();
            }

            @Override
            public void bindView(View view, int index) {
                ImageView viewById = view.findViewById(R.id.card_image_view);
                ToolsKit.setImgActivity(FpCunInfoActivity.this, arrayList.get(index), viewById);
            }

            @Override
            public Object getItem(int index) {
                return arrayList.get(index);
            }
        });

    }

    @OnClick({R.id.back, R.id.sd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sd:
                break;
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, fpIncludeCunTitle);//沉浸黑字
        StatusBarKit.setMargin(this, fuPinRefresh);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        fuPinInFoRecycle.addItemDecoration(new GridSpacingItemDecoration(
                2, SizeUtils.dp2px(12),
                true));

        fuPinInFoRecycle.setLayoutManager(gridLayoutManager);
        initData();
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();

    }
}
